<template>
  <view>
    <view class="goods-list" >
      <view v-for="(goods,index) in goodsList" :key="index" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view> 
  
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数对象
        queryObj:{
          //查询关键字
          query:'',
          //商品分类Id
          cid:'',
          //页码值
          pagenum:1,
          //每页展示多少条数据
          pageszie:10
        },
        goodsList:[],//商品列表数据
        total:0, //总数量，用来实现分页
        isLoading:false, //节流阀 
      };
    },
    onLoad(options) {
      //保存页面跳转传递过来的参数
      this.queryObj.query = options.query||''
      this.queryObj.cid = options.cid||''
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(cb){
        this.isLoading = true
        const {data : res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        this.isLoading = false
       //发送请求结束后  执行回调
       cb&&cb()
        if(res.meta.status==200){
          this.goodsList=[...this.goodsList,...res.message.goods]
          //保存好总数量
          this.total=res.message.total
        }else{
          uni.$showMsg('出差啦~')
        }
      },
      //点击商品 进去 Detail页面
      gotoDetail(goods){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id  
        })
      }
    },
    //下拉触底事件
    onReachBottom() {
    if(this.isLoading)  return  
    const {pagenum,pageszie} = this.queryObj
    if( pagenum*pageszie>=this.total) return uni.$showMsg('数据已加载完毕~')
      this.queryObj.pagenum+=1
      this.getGoodsList()
    },
    //下拉刷新重置参数
    onPullDownRefresh() {
      this.queryObj.pagenum=1
      this.isLoading=false
      this.total=0
      this.goodsList=[]
      this.getGoodsList(()=>{
        uni.stopPullDownRefresh()
        uni.$showMsg('刷新成功')
      })
    }
  }
</script>

<style lang="scss">

</style>
